var Promise = require('bluebird');
var uuid = require('node-uuid');
var crypto = require('crypto');
function md5(str) {
    var md5sum = crypto.createHash('md5');
    md5sum.update(str);
    str = md5sum.digest('hex');
    return str;
};

function generalShortUrl(url) {
    //可以自定义生成MD5加密字符传前的混合KEY   
    var key = "sioeye";
    //要使用生成URL的字符   
    var chars = [
        "a", "b", "c", "d", "e", "f", "g", "h",
        "i", "j", "k", "l", "m", "n", "o", "p",
        "q", "r", "s", "t", "u", "v", "w", "x",
        "y", "z", "0", "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "A", "B", "C", "D",
        "E", "F", "G", "H", "I", "J", "K", "L",
        "M", "N", "O", "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z"

    ];
    //对传入网址进行MD5加密   
    var hex = md5(key + url);

    var resUrl = [];

    for (var i = 0; i < 4; i++) {
        //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算   
        var hexint = 0x3FFFFFFF & parseInt("0x" + hex.substring(i * 8, 8), 16);
        var outChars = '';
        for (var j = 0; j < 6; j++) {
            //把得到的值与0x0000003D进行位与运算，取得字符数组chars索引   
            var index = 0x0000003D & hexint;
            //把取得的字符相加   
            outChars += chars[index];
            //每次循环按位右移5位   
            hexint = hexint >> 5;
        }
        //把字符串存入对应索引的输出数组   
        resUrl[i] = outChars;
    }

    return resUrl[0];
}

module.exports = {
    getLoginInfo: function (username, password) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .select('*')
                .where({ username: username, password: password })
                .then(function (result) {
                    if (result.length > 0) {
                        resolve(result);
                    } else {
                        reject(result);
                    }
                }).catch(function (e) {
                    reject(e);
                });
        });
    },
    getMember: function (uid) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .select('*')
                .where({ id: uid })
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        });
    },
    getList: function (userid) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .select('*')
                .orderBy('createdate', 'desc')
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        });
    },
    insert: function (data) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .insert({
                    username: data.username,
                    password: data.password,
                    uname: data.uname,
                    pwd: data.pwd,
                    company: data.company
                })
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    console.log(e);
                    reject(e);
                });
        });
    },
    statusChange: function (uid, status) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .update({
                    status: status
                })
                .where({ id: uid })
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        })
    },
    resetPassword: function (uid) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .update({
                    password: generalShortUrl(uuid.v4().replace(new RegExp('-', 'gm'), '') + uid)
                })
                .where({ id: uid })
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        })
    },
    changepassword: function (user) {
        return new Promise(function (resolve, reject) {
            knex('users')
                .update({
                    password: user.password
                })
                .where({ id: user.uid })
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        })
    }
};
